package cn.aj.system.controller;
import cn.aj.basic.utils.AjaxResultUtil;
import cn.aj.basic.utils.PageListUtil;
import cn.aj.system.domain.Detail;
import cn.aj.system.query.DetailQuery;
import cn.aj.system.service.DetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
@Api(value = "字典数据的api", description = "字典数据的crud以及其它功能")
public class DetailController {

    @Autowired
    private DetailService detailService;
    
    /**
    * @Description: TODO
    * @Author: aj
    * @Version: 1.0
    * @Date:  2022/11/18 0018 10:16
     * 明细查询全部
    */
    @GetMapping
    @ApiOperation(value = "字典数据查询全部")
    public List<Detail> loadAll() {
        return detailService.loadAll();
    }

   /**
   * @Description: TODO
   * @Author: aj
   * @Version: 1.0
   * @Date:  2022/11/18 0018 10:16
    * 明细分页以及高级查询
   */
    @PostMapping
    @ApiOperation(value = "字典数据分页查询")
    public PageListUtil<Detail> loadPage(@ApiParam(value="字典数据封装的每页条数以及页码",required = true) @RequestBody DetailQuery detailQuery) {
        PageListUtil<Detail> list = detailService.loadPage(detailQuery);
        return list;
    }

    /**
    * @Description: TODO
    * @Author: aj
    * @Version: 1.0
    * @Date:  2022/11/18 0018 10:16
     * 明细根据id查询
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "字典数据根据id查询")
    public Detail getById(@ApiParam(value="字典数据的id",required = true) @PathVariable("id") Long id) {
        return detailService.getById(id);
    }

  /**
  * @Description: TODO
  * @Author: aj
  * @Version: 1.0
  * @Date:  2022/11/18 0018 10:16
   * 明细删除
  */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "字典数据删除")
    public AjaxResultUtil delete(@ApiParam(value="字典数据id",required = true) @PathVariable("id") Long id) {
        detailService.delete(id);
        return AjaxResultUtil.getAjaxResult().success();
    }

    /**
    * @Description: TODO
    * @Author: aj
    * @Version: 1.0
    * @Date:  2022/11/18 0018 10:16
     * 明细新增或者修改
    */
    @PutMapping
    @ApiOperation(value = "字典数据新增或者修改")
    public AjaxResultUtil addOrUpdate(@ApiParam(value="字典数据实体类",required = true) @RequestBody Detail detail) {
        if (detail.getId() == null) { // 新增
            detailService.add(detail);
        } else { // 修改
            detailService.update(detail);
        }
        return AjaxResultUtil.getAjaxResult().success();
    }

    /**
    * @Description: TODO
    * @Author: aj
    * @Version: 1.0
    * @Date:  2022/11/18 0018 10:16
     * 明细批量删除
    */
    @PatchMapping
    @ApiOperation(value = "字典数据批量删除")
    public AjaxResultUtil batchDelete(@ApiParam(value="字典数据的id",required = true) @RequestBody List<Long> ids) {
        detailService.batchDelete(ids);
        return AjaxResultUtil.getAjaxResult().success();
    }

    /**
    * @Description: TODO
    * @Author: aj
    * @Version: 1.0
    * @Date:  2022/11/18 0018 10:16
     * 根据明细外键查询所属当前数据
    */
    @GetMapping("/loadType/{id}")
    public List<Detail> loadTypeId(@PathVariable("id") Long id) {
        return detailService.loadTypeId(id);
    }
}
